package storeMovie.Util;

import java.util.List;

import org.hibernate.*;

import storeMovie.JavaBean.User;

public class UserManager {

	// private Session sessionUser = null;
	public UserManager() {
		// sessionUser=
		// SessionFactoryUtil.getSessionFactory().getCurrentSession();
		// sessionUser.beginTransaction();
	}

	public String checkLogin(String username, String password) {
		Session session = null;
		String userName = "";
		try {
			session = HibernateUtil.currentSession();
			Query query = session
					.createQuery("from User where Username=? and password=?");
			query.setParameter(0, username);
			query.setParameter(1, password);

			List<User> listUser = query.list();
			User user = listUser.get(0);
			return user.getUserName();
		} catch (Exception ex) {
			System.out.println(ex.getCause());// TODO: handle exception
		}
		return userName;
	}

	public User getUser(String userName) {
		Session session = null;
		User user = null;
		try {
			session = HibernateUtil.currentSession();
			user = (User) session.get(User.class, new String(userName));
			return user;
		} catch (Exception ex) {
			System.out.println(ex.getCause());
		}
		return user;
	}

	public void insertUser(User user) {
		Session session=null;
		Transaction t = null;
		try{
			session=HibernateUtil.currentSession();
			t=session.beginTransaction();
			session.save(user);
			t.commit();
		}catch (Exception e) {
			e.printStackTrace();
			t.rollback();
		}
	}
}
